package com.kingland.zf.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResponsiveAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request , HttpServletResponse response , Authentication authentication) throws IOException, ServletException {
        response.setStatus ( HttpServletResponse.SC_OK );
        PrintWriter out =response.getWriter ();
        response.setContentType ( "application/json" );
        response.setCharacterEncoding ( "utf-8" );
        out.print(new ObjectMapper ().writeValueAsString(authentication));
        out.flush();
    }
}
